<?php

namespace app\<app>\config\<controller>;
use app\common\service\Validate as ValidateService;
use app\common\service\Power as PowerService;
use app\common\service\Logic as LogicService;
use app\<app>\config\<controller>\Info as ThatInfo;

class Logic
{
    public function Mode()
    {
        $ThatInfo = new ThatInfo();
        return $ThatInfo->Model();
    }

    public function Add($params = [])
    {
        $Model = $this->Mode();
        $ThatInfo = new ThatInfo();
        $LogicService = new LogicService();
        $ValidateService = new ValidateService();
        $PowerService = new PowerService();
        $InfoForm = $ThatInfo->InfoForm();
        $InfoMode = $ThatInfo->InfoMode();
        $fields = $LogicService->AllField($InfoForm);
        $ItemData = $LogicService->GetData('add', $params, $params, $fields,$Model);
        $check = $ValidateService->Rule($LogicService->Validate($InfoForm), $ItemData, $Model, $InfoMode, 0);
        if ($check['code'] == '203') {
            return $check;
        }
        $ItemData=$PowerService->FormField($InfoForm['power_field'],$ItemData);
        $ItemData['a']['create_id'] = GetToken('id');
        $ItemData['a']['update_id'] = $ItemData['a']['create_id'];
        $ItemData['a']['update_time'] = date('Y-m-d H:i:s');
        $ItemData['a']['create_time'] = $ItemData['a']['update_time'];
        $back['id'] = $Model->insertGetId($ItemData['a']);
        $LogicService->SubTable($ItemData, $Model, $back['id'], $InfoMode);//更新子表
        return ['code' => 200, 'msg' => '新增成功', 'data' => $back];
    }

    public function Edit($params = [])
    {

        if (empty($params['id'])) {
            return ['code' => 203, 'msg' => '参数ID不能为空', 'data' => []];
        }
        $Model = $this->Mode();
        $ThatInfo = new ThatInfo();
        $LogicService = new LogicService();
        $ValidateService = new ValidateService();
        $PowerService = new PowerService();
        $InfoForm = $ThatInfo->InfoForm();
        $InfoMode = $ThatInfo->InfoMode();
        $fields = $LogicService->AllField($InfoForm);
        $ItemData = $LogicService->GetData('edit', $params, $params, $fields);
        $check = $ValidateService->Rule($LogicService->Validate($InfoForm), $ItemData, $Model, $InfoMode, $params['id']);
        if ($check['code'] == '203') {
            return $check;
        }
        $find = $Model->where("`id`='" . $params['id'] . "'")->count();
        if ($find == 0) {
            return ['code' => 203, 'msg' => '无此数据', 'data' => []];
        }
        $ItemData=$PowerService->FormField($InfoForm['power_field'],$ItemData);
        $ItemData['a']['update_id'] = GetToken('id');
        $ItemData['a']['update_time'] = date('Y-m-d H:i:s');
        $Model->where("`id`='" . $params['id'] . "'")->Update($ItemData['a']);
        $back['id'] = $params['id'];
        $LogicService->SubTable($ItemData, $Model, $back['id'], $InfoMode);//更新子表
        return ['code' => 200, 'msg' => '修改成功', 'data' => $back];
    }

    public function Delete($params = [])
    {
        if (empty($params['id']) && empty($params['ids'])) {
            return ['code' => 203, 'msg' => '参数不能为空', 'data' => []];
        }
        $Model = $this->Mode();
        $ThatInfo = new ThatInfo();
        $LogicService = new LogicService();
        $InfoMode = $ThatInfo->InfoMode();
        return $LogicService->Delete($Model, $params, $InfoMode);
    }

    public function Detail($params = [])
    {
        $Model = $this->Mode();
        $ThatInfo = new ThatInfo();
        $LogicService = new LogicService();
        $PowerService = new PowerService();
        $InfoForm = $ThatInfo->InfoForm();
        $InfoMode = $ThatInfo->InfoMode();
        $fields = $LogicService->AllField($InfoForm);
        $field = $LogicService->FiledStr($fields, 'detail', 'a');
        $find = [];
        if (!empty($params['id'])) {
            $find = $Model->where("`id`='" . $params['id'] . "'")->field($field . ',id')->findOrEmpty()->toArray();
            if (empty($find)) {
                return ['code' => 203, 'msg' => '无此数据', 'data' => []];
            }
        }
        $detail=$LogicService->Detail($find, $params, $InfoForm, $InfoMode);
        $data['source'] = $LogicService->GetSource($InfoForm);
        $power=$PowerService->FormField($InfoForm['power_field'],$detail);
        $data['detail']=$power['detail'];
        $data['power']=$power['power'];
        return ['code' => 200, 'msg' => '请求成功', 'data' => $data];
    }

    public function UpdateField($params = [])
    {
        if (empty($params['id'])) {
            return ['code' => 203, 'msg' => '参数id不能为空', 'data' => []];
        }
        if (empty($params['field'])) {
            return ['code' => 203, 'msg' => '参数field不能为空', 'data' => []];
        }
        $Model = $this->Mode();
        $ThatInfo = new ThatInfo();
        $LogicService = new LogicService();
        $InfoLists = $ThatInfo->InfoLists();
        $InfoMode = $ThatInfo->InfoMode();
        return $LogicService->UpdateField($Model, $params, $InfoLists, $InfoMode);
    }

}